package com.ht.medicaltreatment.activity

import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.ClickUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.SampleDetailAdapetr
import com.ht.medicaltreatment.bean.CentrifugeSample
import com.ht.medicaltreatment.viewmodel.CentrifugeDetailViewModel
import com.xgst.lib.utils.showToast
import kotlinx.android.synthetic.main.activity_centrifuge_detail_layout.*
import kotlinx.android.synthetic.main.activity_centrifuge_detail_layout.cbAll
import kotlinx.android.synthetic.main.activity_centrifuge_detail_layout.recyclerView
import kotlinx.android.synthetic.main.activity_centrifuge_detail_layout.tvCheckCount

class CentrifugeDetailActivity : BaseActivity(), OnItemClickListener, View.OnClickListener {
    override fun getLayoutId() = R.layout.activity_centrifuge_detail_layout

    private val model by viewModels<CentrifugeDetailViewModel>()
    private var adapter = SampleDetailAdapetr()
    private var itemAllCount = 0
    private var itemCheckCount = 0
    private var listPosition = mutableListOf<CentrifugeSample>()

    override fun init() {
        setTitle("离心机样本")
        recyclerView.layoutManager = LinearLayoutManager(this)
        recyclerView.adapter = adapter
        adapter.setOnItemClickListener(this)
        val centrifugeId = intent.getIntExtra("centrifugeId",-1)
        cbAll.setOnClickListener(this)
        btnRemove.setOnClickListener(this)

        ClickUtils.expandClickArea(cbAll,70)
        showProgress()
        model.centrifugeInfo("$centrifugeId",{
            dismissProgress()
            itemAllCount = it!!.size
            tvSampleCount.text = "$itemAllCount"
            adapter.setNewInstance(it)
        },{
            dismissProgress()
            showToast(it)
        })
    }

    override fun onItemClick(adapter: BaseQuickAdapter<*, *>, view: View, position: Int) {
        val item = adapter.data[position] as CentrifugeSample
        if(item.isCheck){
            item.isCheck = false
            if(cbAll.isChecked){
                cbAll.isChecked = false
            }
            itemCheckCount--
            listPosition.remove(item)
        }else{
            item.isCheck = true
            itemCheckCount++
            if(itemCheckCount == itemAllCount){
                cbAll.isChecked = true
            }
            listPosition.add(item)
        }
        tvCheckCount.text = "$itemCheckCount"
        adapter.notifyItemChanged(position)
    }

    override fun onClick(v: View?) {
        when(v){
            cbAll -> {
                if(cbAll.isChecked){
                    adapter.data.forEach {
                        it.isCheck = true
                    }
                    itemCheckCount = itemAllCount

                    listPosition.addAll(adapter.data)

                }else{
                    adapter.data.forEach {
                        it.isCheck = false
                    }
                    itemCheckCount = 0
                    listPosition.clear()
                }
                tvCheckCount.text = "$itemCheckCount"
                adapter.notifyDataSetChanged()
            }

            btnRemove -> {
                if(itemCheckCount == 0){
                    return
                }
                showProgress()
                var ids:String? = null
                adapter.data.forEach {
                    if(it.isCheck){
                        if(ids == null){
                            ids = "${it.id}"
                        }else{
                            ids = "$ids,${it.id}"
                        }
                    }
                }
                model.deleteSample(ids!!,{
                    dismissProgress()
                    listPosition.forEach {
                        adapter.remove(it)
                    }
                    listPosition.clear()
                    tvSampleCount.text = "${adapter.data.size}"
                    tvCheckCount.text = "0"
                },{
                    dismissProgress()
                    showToast(it)
                })
            }
        }
    }

}
